//
//  SCHopeController.m
//  Mutal
//
//  Created by TonyStart on 16/10/18.
//  Copyright © 2016年 com.wx. All rights reserved.
//

#import "SCHopeController.h"
#import "SCWishCell.h"
#import "SCWishViewModel.h"
#import "SCCoverView.h"
#import "SCWishInfoController.h"
@interface SCHopeController ()<UITableViewDelegate,UITableViewDataSource>{
    NSInteger _requestType;
}

@property(nonatomic,strong)UISegmentedControl *segmContr;
@property(nonatomic,strong)SCWishViewModel *wishViewModel;
@end
#define SCREEN_SIZE [UIScreen mainScreen].bounds.size
@implementation SCHopeController

- (void)viewDidLoad {
    [super viewDidLoad];
//    NSLog(@"viewDidLoad:%@",self.tableView);
//    [self setAutomaticallyAdjustsScrollViewInsets:false];
//    [self setEdgesForExtendedLayout:UIRectEdgeNone];
//    //让导航栏不透明且占空间位置，所以我们的坐标就会从导航栏下面开始算起
//    self.tabBarController.tabBar.translucent = NO;
//    self.navigationController.navigationBar.translucent = NO;
    
    [self setupUI];
    
}

-(void)setupUI{
    UINib *nib = [UINib nibWithNibName:@"SCWishCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"threeCell"];
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    _requestType = 0;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.titleView = self.segmContr;
    [self.refReshC addTarget:self action:@selector(loadWishData) forControlEvents:UIControlEventValueChanged];
    [self loadWishData];
    
}

-(void)loadWishData{
    [self.refReshC beginRefreshing:^(BOOL is_drop) {
        if (is_drop) {
            self.refReshLoadType = dropLoad;
            _page = 1;
        }else{
            self.refReshLoadType = pullLoad;
            if (self.isloadMore) {
                _page += 1;
            }
        }
    }];
    
    [self.wishViewModel requestWishsDataWithHot:[NSString stringWithFormat:@"%ld",_requestType] loadType:self.refReshLoadType page:_page finish:^(BOOL isLoad) {
        self.isloadMore = isLoad;//改变标识 如果为NO不能再对page进行增加了 每次请求都请求上次的page直接有数据出现
        
        //如果请求到的数据是个空数据 说明已经到底了 没有更多的数据了
        if (!self.isloadMore) {
            [SCAlertView alertViewWithTitle:@"没有更多数据了"];
            [self.refReshC endRefreshing];
            return;
        }
        [self.tableView reloadData];
        [self.refReshC endRefreshing];

    }];
 
}

-(SCWishViewModel *)wishViewModel{
    if (!_wishViewModel) {
        _wishViewModel = [[SCWishViewModel alloc] init];
    }
    return _wishViewModel;
}

-(UISegmentedControl *)segmContr{
    if (!_segmContr) {
        _segmContr = [[UISegmentedControl alloc] initWithItems:@[@"最新愿望",@"热门愿望"]];
        _segmContr.frame = CGRectMake(0, 0, SCREEN_SIZE.width, 40);
        //前景色透明 这样边框就没有了
        _segmContr.tintColor = [UIColor clearColor];
        //定义选中状态的样式selected，类型为字典
        NSDictionary *selected = @{NSFontAttributeName:[UIFont systemFontOfSize:20],
                                   NSForegroundColorAttributeName:[UIColor blackColor]};
        //定义未选中状态下的样式normal，类型为字典
        NSDictionary *normal = @{NSFontAttributeName:[UIFont systemFontOfSize:15],
                                 NSForegroundColorAttributeName:[UIColor whiteColor]};
        
        [_segmContr setTitleTextAttributes:normal forState:UIControlStateNormal];
        [_segmContr setTitleTextAttributes:selected forState:UIControlStateSelected];
        
        [_segmContr setSelectedSegmentIndex:0];
        
        [_segmContr addTarget:self action:@selector(switchingView) forControlEvents:UIControlEventValueChanged];
        
    }
    return _segmContr;
}
//segmControl的点击事件
- (void)switchingView
{
    switch (self.segmContr.selectedSegmentIndex) {
        case 0:
            _requestType = 0;
            break;
        case 1:
            _requestType = 1;
            break;
    }
    [self removeTableViewData];
    [self loadWishData];
}
-(void)removeTableViewData{
    self.wishViewModel.wishArray = [NSMutableArray array];
    [self.tableView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.wishViewModel.wishArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"threeCell";
    
    SCWishCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    cell.wishModel = self.wishViewModel.wishArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SCWishInfoController *wishInfoVC = [UIStoryboard storyboardWithName:@"SCWishInfo" bundle:nil].instantiateInitialViewController;
    
    wishInfoVC.wish = self.wishViewModel.wishArray[indexPath.row];
    wishInfoVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:wishInfoVC animated:YES];
}


@end
